import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.Timer;


public class Tester extends JFrame
{
	private static final long serialVersionUID = -6411014878802068316L;
	private Vector2D center0;
	private Vector2D velRef0;
	private Vector2D center1;
	private Vector2D velRef1;
	private BillardBall ball0, ball1;

	private final int RADIUS = 20;
	
	public Tester()
	{
		class TimerListener implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				Simulation.advanceTime(1);
				drawBalls();
			}
			
		}
		final Timer t = new Timer(100, new TimerListener());

		class MEvents extends MouseAdapter
		{
			public void mouseClicked(MouseEvent e)
			{
				if (center0==null && velRef0 == null)
				{
					center0 = new Vector2D(e.getX(),e.getY());
				}
				else if (center0 != null && velRef0 == null)
				{
					velRef0 = new Vector2D(e.getX(),e.getY());
					drawCircles();
				}
				else
				{
					if (center1==null && velRef1 == null)
					{
						center1 = new Vector2D(e.getX(),e.getY());
					}
					else if (center1 != null && velRef1 == null)
					{
						velRef1 = new Vector2D(e.getX(),e.getY());
						drawCircles();
					}
					else
					{
						ball0 = new BillardBall();
						ball0.setRadius(RADIUS);
						ball0.setPosition(center0);
						ball0.setVelocity(velRef0.substract(center0));
						ball1 = new BillardBall();
						ball1.setRadius(RADIUS);
						ball1.setPosition(center1);
						ball1.setVelocity(velRef1.substract(center1));
						Simulation.start(ball0, ball1);
						t.start();
					}
				}
			}
			
			public void mouseMoved(MouseEvent e)
			{
				if (center1==null || velRef1 == null)
				{
				drawSketch(e.getX(),e.getY());
				}
			}
		}
		MEvents e = new MEvents();
		this.addMouseListener(e);
		this.addMouseMotionListener(e);
	}
	
	public void drawSketch(int x, int y)
	{
		Graphics2D g2 = (Graphics2D)this.getGraphics();
		g2.setColor(Color.RED);
		g2.clearRect(0, 0, this.getWidth(), this.getHeight());
		

		
		if (center0==null && velRef0 == null)
		{

		}
		else if (center0 != null && velRef0 == null)
		{
			int cx = (int) (center0.x - RADIUS);
			int cy = (int) (center0.y - RADIUS);
			
			g2.drawOval(cx, cy, 2*RADIUS, 2*RADIUS);
			g2.drawLine((int) center0.x, (int)center0.y, x, y);
		}
		else
		{
			if (center1==null && velRef1 == null)
			{

			}
			else if (center1 != null && velRef1 == null)
			{
				int cx = (int) (center1.x - RADIUS);
				int cy = (int) (center1.y - RADIUS);
				
				g2.drawOval(cx, cy, 2*RADIUS, 2*RADIUS);
				g2.drawLine((int)center1.x, (int)center1.y, x, y);
			}
		}
		drawCircles();
	}
	
	public void drawCircles()
	{
		if (center0 != null && velRef0 != null)
		{
			int x = (int) (center0.x - RADIUS);
			int y = (int) (center0.y - RADIUS);
			Graphics2D g2 = (Graphics2D)this.getGraphics();
			g2.setColor(Color.BLUE);
			g2.fillOval(x, y, 2*RADIUS, 2*RADIUS);
			g2.drawLine((int)center0.x, (int)center0.y, (int)velRef0.x, (int)velRef0.y);
		}
		if (center1 != null && velRef1 != null)
		{
			int x = (int) (center1.x - RADIUS);
			int y = (int) (center1.y - RADIUS);
			Graphics2D g2 = (Graphics2D)this.getGraphics();
			g2.setColor(Color.BLUE);
			g2.fillOval(x, y, 2*RADIUS, 2*RADIUS);
			g2.drawLine((int)center1.x, (int)center1.y, (int)velRef1.x, (int)velRef1.y);
		}
	}
	
	public void drawBalls()
	{
		Graphics2D g2 = (Graphics2D)this.getGraphics();
		g2.setColor(Color.BLUE);
		g2.fillOval((int)ball0.getPosition().x-ball0.getRadius(), (int)ball0.getPosition().y-ball0.getRadius(), 2*ball0.getRadius(), 2*ball0.getRadius());
		g2.fillOval((int)ball1.getPosition().x-ball1.getRadius(), (int)ball1.getPosition().y-ball1.getRadius(), 2*ball1.getRadius(), 2*ball1.getRadius());

	}
	
	public static void main(String[] args)
	{
		Tester t = new Tester();
		t.setVisible(true);
		t.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

}
